<?php
class UserDAO extends DAO{
	
	private $tableName = "tb_user";
	
	private $conn;
	
	private $sql;
	
	private $prefixo = "";

	public function __construct() {
	
		$this->conn = new CONEXAO();
		
	}
	
	public function mapear($obj){
		
		$array = $obj->toArray();

		$this->conn->Conecta();
		
		if(!isset($array['id']) || $array['id']==0){
		$array['id'] = $this->conn->maxId($this->tableName,$this->prefixo) + 1;
		}
		$this->conn->Desconecta();

		return $array;
	}
	
	 public function inserir($obj){
	 	
	 	 if(!$obj instanceof User){
	 	
	 		throw(new Exception("Dados Inapropriados"));
	 	}

	 	$tableName = $this->tableName;
	 	
			
	 	$comando = DMLGenerator::createInsert($tableName,$this->mapear($obj),$this->prefixo);

	 	$this->conn->Conecta();
	 	$this->conn->Executa($comando);
	 	$this->conn->Desconecta();
	 	
	 	
	 }
	
	 public function buscar($id){
	 	
	 	$this->sql = new SQLGenerator($this->tableName);
	 	$this->sql->setWhere(array("id = $id"));

	 	$this->conn->Conecta();
	 	
	 	$result = $this->conn->getResultAsVector($this->sql->generateSQL());
	 	
	 	$this->conn->Desconecta();
	 	
	 	if(count($result)==1){
	 	
	 	$obj = User::construct($result[0]);
	 	
	 	return $obj;
	 	}
	 	else{
	 		throw(new Exception("Nenhum objeto encontrado"));
	 		return null;
	 	}
	 	
	 }
	 
	 public function buscarTodosSerach($obj_pesquisa){
	 
	 	if($obj_pesquisa instanceof UserSearch){
	 		
	 		$this->sql = new SQLGenerator($this->tableName);
	 
	 		$this->sql->setWhere($this->geraWherePesquisa($obj_pesquisa));
	 
	 		//echo $this->sql->generateSQL();
	 
	 		$this->conn->Conecta();
	 
	 		$result = $this->conn->getResultAsVector($this->sql->generateSQL());
	 
	 		$this->conn->Desconecta();
	 
	 		$objArray = array();
	 
	 		foreach ($result as $array){
	 
	 			$objArray[] = User::construct($array);
	 
	 		}
	 
	 		return $objArray;
	 
	 	}
	 	else{
	 
	 		throw(new Exception("Dados Inapropriados"));
	 	}
	 }
	
	 public function buscarTodos($obj_pesquisa){
	 	
	 	if($obj_pesquisa instanceof User){
	 	$this->sql = new SQLGenerator($this->tableName);
	 	
	 	$arrayWhere = array();
	 	$attr = $obj_pesquisa->toArray();
	 	
	 	
	 	foreach ($attr as $key => $value){
	 		
	 		if($value !== 0 && $value !== '0' && $value !== '' && $value !== '0000-00-00 00:00:00' && $value !== '0000-00-00' ){
	 			
	 			$arrayWhere[] = "".$key." = '".$value."'";
	 		}
	 	}
	 	
	 	$this->sql->setWhere($arrayWhere);
	 	
	 	//echo $this->sql->generateSQL();
	 	
	 	$this->conn->Conecta();
	 	 
	 	$result = $this->conn->getResultAsVector($this->sql->generateSQL());
	 	 
	 	$this->conn->Desconecta();
	 	
	 	$objArray = array();
	 	
	 	foreach ($result as $array){
	 		
	 		$objArray[] = User::construct($array);
	 		
	 	}
	 	
	 	return $objArray;
	 	
	 	}
	 	else{
	 		
	 		throw(new Exception("Dados Inapropriados"));
	 	}
	 }
	
	 public function atualizar($obj){
	 	
	 	if(!$obj instanceof User){
	 	
	 		throw(new Exception("Dados Inapropriados"));
	 	}
	 	
	 	$tableName = $this->tableName;
	 	
	 	
	 	$comando = DMLGenerator::createUpdate($tableName,$this->mapear($obj),$this->prefixo);
	 	
	 	$this->conn->Conecta();
	 	$this->conn->Executa($comando);
	 	$this->conn->Desconecta();
	 	
	 	
	 }
	
	 public function apagar($obj){
	 	if(!$obj instanceof User){
	 	
	 		throw(new Exception("Dados Inapropriados"));
	 	}
	 	
	 	$tableName = $this->tableName;
	 	
	 	
	 	$comando = DMLGenerator::createDelete($tableName,$obj->getId(),$this->prefixo);
	 	
	 	$this->conn->Conecta();
	 	$this->conn->Executa($comando);
	 	$this->conn->Desconecta();
	 	
	 	
	 }
	
	 public function contem($obj_pesquisa){
	 	
	 	if($obj_pesquisa instanceof UserSearch){
	 		
	 		$this->sql = new SQLGenerator($this->tableName);

	 		$this->sql->setWhere($this->geraWherePesquisa($obj_pesquisa));
	 		
	 		$this->sql->setFields(array("count(*) count"));
	 	
	 		//echo $this->sql->generateSQL();
	 	
	 		$this->conn->Conecta();
	 	
	 		$result = $this->conn->getResultAsVector($this->sql->generateSQL());
	 	
	 		$this->conn->Desconecta();
	 	
	 		return $result[0]['count'];
	 	
	 	}
	 	else{
	 	
	 		throw(new Exception("Dados Inapropriados"));
	 	}
	 	
	 }
	 
	 private function geraWherePesquisa($obj_pesquisa){
	 	
	 	if($obj_pesquisa instanceof UserSearch){
	 	
	 		$arrayWhere = array();
	 		$attr = $obj_pesquisa->toArray();
	 	
	 		foreach ($attr as $key => $value){
	 			if($value!= null){
	 	
	 				//se tiver o termo min
	 				if (strpos($key,'Min') !== false) {
	 					$arrayWhere[] = "".str_replace("Min", "",$key)." >= '".$value."'";
	 				}
	 				//se tiver o termo max
	 				elseif (strpos($key,'Max') !== false) {
	 					$arrayWhere[] = "".str_replace("Max", "", $key)." <= '".$value."'";
	 				}
	 				//se for string
	 				elseif(is_string($value)){
	 					$arrayWhere[] = "".$key." like '%".$value."%'";
	 	
	 				}
	 				//se for numero
	 				else{
	 					$arrayWhere[] = "".$key." = '".$value."'";
	 				}
	 			}
	 		}
	 		
	 		return $arrayWhere;
	 	
	 	}
	 	else{
	 	
	 		throw(new Exception("Dados Inapropriados"));
	 	}
	 }
	
	
}



?>